﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace Notoric.Gui.Controls.Adorners
{
    /// <summary>
    /// Implements an adorner that displays the size of the adorned element.
    /// </summary>
    /// <remarks>
    /// This code was taken from the "WPF Diagram Designer: Part 1" article on CodeProject.com written by "sukram".
    /// See <a href="http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx">http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part1.aspx</a>
    /// </remarks>
    /// <seealso cref="SizeChrome"/>
    public class SizeAdorner : Adorner
    {
        #region Constructors

        /// <summary>
        /// Creates a new <see cref="SizeAdorner"/> instance.
        /// </summary>
        /// <param name="adornedElement">The adorned element</param>
        public SizeAdorner(FrameworkElement adornedElement)
            : base(adornedElement)
        {
            this.SnapsToDevicePixels = true;
            this.adornedElement = adornedElement;
            this.chrome = new SizeChrome();
            this.chrome.DataContext = adornedElement;
            this.visuals = new VisualCollection(this);
            this.visuals.Add(this.chrome);
        }

        #endregion

        #region Overrides

        /// <inheritdoc/>
        protected override int VisualChildrenCount
        {
            get
            {
                return this.visuals.Count;
            }
        }

        /// <inheritdoc/>
        protected override Visual GetVisualChild(int index)
        {
            return this.visuals[index];
        }

        /// <inheritdoc/>
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            this.chrome.Arrange(new Rect(new Point(0.0, 0.0), arrangeBounds));
            return arrangeBounds;
        }

        #endregion

        #region Private Fields

        /// <summary>
        /// Holds the adorner chrome.
        /// </summary>
        private SizeChrome chrome;
        /// <summary>
        /// Holds the collection of visual childs of the adorner.
        /// </summary>
        private VisualCollection visuals;
        /// <summary>
        /// Holds a reference to the adorned element.
        /// </summary>
        private FrameworkElement adornedElement;

        #endregion
    }
}